package com.atguigu.spzx.user.service.impl;

import com.atguigu.spzx.common.constant.RedisKeyConstant;
import com.atguigu.spzx.user.manager.SmsManager;
import com.atguigu.spzx.user.service.SmsService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * SmsServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2023/11/4
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private SmsManager smsManager;

    /**
     * 发送短信验证码
     * @param phone
     */
    @Override
    public void sendCode(String phone) {
        //1.生成验证码
        String code = RandomStringUtils.randomNumeric(4);

        //2.验证码存储到redis
        redisTemplate.opsForValue().set(RedisKeyConstant.USER_REGISTER_PHONE_PREFIX + phone,
                code, RedisKeyConstant.USER_REGISTER_PHONE_EXPIRE, TimeUnit.MINUTES);

        //3.向手机发送验证码
        //smsManager.sendCode(phone, code);
    }
}
